package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.minio.MinIOService;
import com.heima.common.util.UserThreadLocalUtil;
import com.heima.media.dto.WmMaterialDto;
import com.heima.media.entity.WmMaterial;
import com.heima.media.mapper.WmMaterialMapper;
import com.heima.media.service.IWmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

/**
 * <p>
 * 自媒体图文素材信息表 服务实现类
 * </p>
 *
 * @author syl
 * @since 2021-11-03
 */
@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements IWmMaterialService {

    // 注入MinIOService
    @Autowired
    private MinIOService minIOService;

    @Override
    public ResponseResult saveWmMaterial(MultipartFile file) {
        // 从本地线程中获取当前的用户id
        User user = UserThreadLocalUtil.get();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        try {
            // 调用minio上传图片
            // 返回图片的url
            String url = minIOService.upload(file);
            // 保存素材表
            WmMaterial material = new WmMaterial();
            material.setUserId(user.getUserId());
            material.setUrl(url);
            material.setType(0);
            material.setIsCollection(false);
            material.setCreatedTime(new Date());
            this.save(material);
            return ResponseResult.okResult(material);

        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
    }

    @Override
    public ResponseResult<WmMaterial> listByPage(WmMaterialDto dto) {
        User user = UserThreadLocalUtil.get();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        LambdaQueryWrapper<WmMaterial> queryWrapper = new LambdaQueryWrapper<>();
        //        分页
        Page<WmMaterial> page = new Page<>(dto.getPage(), dto.getSize());

        if (dto.getIsCollection() != null) {
            queryWrapper.eq(WmMaterial::getIsCollection, dto.getIsCollection());
        }
        //        隐藏条件： 当前登录人  排序
        queryWrapper.eq(WmMaterial::getUserId, user.getUserId());
        queryWrapper.orderByDesc(WmMaterial::getCreatedTime);

        Page<WmMaterial> page1 = this.page(page, queryWrapper);

        return new PageResponseResult(dto.getPage(), dto.getSize(), page1.getTotal(), page1.getRecords());
    }

    @Override
    public ResponseResult updatecollect(Integer id, int i) {
        if (i == 1) {
            this.update().set("is_collection", true).eq("id", id).update();
            return ResponseResult.okResult();
        } else if (i == 2) {
            this.update().set("is_collection", false).eq("id", id).update();
            return ResponseResult.okResult();
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }
}
